//
//  MenuView.m
//  Menu
//
//  Created by ZF on 16/12/21.
//  Copyright © 2016年 ZhuFeng. All rights reserved.
//

#import "MenuView.h"
#import "MenuTableViewCell.h"
#define DefaultWidth 80
#define DefaultHight 40
#define ArrowHight 10



@interface MenuView ()<UITableViewDelegate, UITableViewDataSource>
{
    CGRect menuRect;
    
    CGFloat cornerRadiu;
    CGFloat arrowPointX;
    
    UIColor *cellBGC;
    UIColor *cellFontC;
    UIColor *separatorC;
}

@property (strong, nonatomic) UITableView *tableView;

@end



@implementation MenuView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

//绘制分隔线
- (void)drawRect:(CGRect)rect
{
   
    NSLog(@"menuView宽高:%f,%f",rect.size.width,rect.size.height);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, cellBGC.CGColor);
    
    CGContextMoveToPoint(context, arrowPointX, 0);
    CGContextAddLineToPoint(context, arrowPointX - 10, ArrowHight);
    CGContextAddLineToPoint(context, arrowPointX + 10, ArrowHight);
    
    CGContextFillPath(context);
    
}

//绘制tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MenuTableViewCell *cell = [[MenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
 
    cell.textLabel.text = _data[indexPath.row];
    cell.textLabel.textColor = cellFontC;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.backgroundColor = cellBGC;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return DefaultHight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate selectCellImplementAtIndexRow:indexPath.row];
}



//初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if(self){
        cornerRadiu = 10.0;

        self.layer.cornerRadius = cornerRadiu;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        self.ifHide = YES;
        self.alpha = 0.0;
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake( 0, ArrowHight, frame.size.width , frame.size.height) style:UITableViewStylePlain];
        
        [self.tableView registerClass:[MenuTableViewCell class] forCellReuseIdentifier:@"cell"];
     
        self.tableView.scrollEnabled = NO;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.layer.cornerRadius = 10.0;
        [self addSubview:self.tableView];
    }
    return self;
}

- (instancetype)initWithSetCellLabelTextWithArray:(NSArray<NSString *> *)array menuPoint:(CGPoint)point withMenuStyle:(MenuStyle)style
{
    [self AddCellLabelTextWithArray:array];
    menuRect.origin.x = point.x;
    menuRect.origin.y = point.y;
    CGRect frame = menuRect;
    
    switch (style) {
        case MenuBlackStyle:
        {
            cellBGC = [UIColor whiteColor];
            cellFontC = [UIColor whiteColor];
            separatorC = [UIColor colorWithRed:192/255.0 green:192/255.0 blue:192/255.0 alpha:1.0];
            
        }break;
        
        case MenuDefaultStyle:
        {
            cellBGC = [UIColor colorWithRed:49/255.0 green:49/255.0 blue:47/255.0 alpha:1.0];
            cellFontC = [UIColor whiteColor];
            separatorC = [UIColor grayColor];
            
        }break;
    }
   
    return [self initWithFrame:frame];
}

//添加数据源
- (void)AddCellLabelTextWithArray:(NSArray<NSString *> *)array
{
    if(!_data)
    {
        _data = [NSMutableArray new];
    }
    for(NSString *str in array)
    {
        [_data addObject:str];
    }
    
    NSString *longestStr = _data[0];
    for(int i = 1; i < _data.count; i++)
    {
        if(_data[i].length > longestStr.length)
        {
            longestStr = _data[i];
        }
    }
    NSLog(@"最长的CellLable：%ld",longestStr.length);
    
    CGRect rect = [longestStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 25) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:25]} context:nil];
    NSLog(@"RectWidth:%f , RectHight:%f",rect.size.width,rect.size.height);
    
    if(rect.size.width > DefaultWidth)
    {
        menuRect.size.width = rect.size.width;
    }
    else menuRect.size.width = DefaultWidth;
    menuRect.size.height = _data.count * DefaultHight + ArrowHight;
    arrowPointX = menuRect.size.width / 2+10;
}

//添加新数据后并修改frame
- (void)repaintMenuViewAddData:(NSArray<NSString *> *)data
{
    [self AddCellLabelTextWithArray:data];
    self.frame = menuRect;
    self.tableView.frame = CGRectMake(0, ArrowHight, menuRect.size.width, menuRect.size.height);
    [self.tableView reloadData];
    [self setNeedsDisplay];
}

- (void)disappear
{
    [UIView animateWithDuration: 0.5 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 0.0;
        self.ifHide = YES;
    } completion:^(BOOL finished) {
        self.superview.hidden = YES;
    }];
}

- (void)display
{
    [UIView animateWithDuration: 0.5 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1.0;
        self.ifHide = NO;
    }];
}



@end
